/*
 * This file is part of the Marmalade SDK Code Samples.
 *
 * (C) 2001-2012 Marmalade. All Rights Reserved.
 *
 * This source code is intended only as a supplement to the Marmalade SDK.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

//-----------------------------------------------------------------------------
// ExampleIwUILocalise
//-----------------------------------------------------------------------------

/**
 * @page ExampleIwUILocalise IwUI Localise Example
 * The following example demonstrates localisation of a simple dialogue.
 *
 * The main classes used to achieve this are:
 *  - CIwUIView
 *  - CIwUIController
 *  - CIwUIElement
 *  - CIwUIStyleSheetManager
 *
 * The main functions used to achieve this are:
 *  - IwUIInit()
 *  - IwUISetLocaliseCallback()
 *  - IwUITerminate();
 *
 * This example demonstrates the loading localised strings and the useage of a
 * callback to provide this data to IwUI.
 *
 * @image html IwUILocaliseImage.png
 *
 * @include IwUILocalise.cpp
 */
#include "IwGx.h"
#include "IwUI.h"

// Project includes
#include "IwUIDailyReport.h"

const char* g_LanguageNames[] =
{
    "english",
    "french"
};

// Resource class to contain data for each localised string
#define RESTYPE_LOCALISED_STRING    "CLocalisedString"

const char* LocaliseCallback(const CIwPropertySet& propertySet);
void ExampleInit();
bool ExampleUpdate(int32 deltaMS);
void ExampleRender();
void ExampleShutDown();

class  CLocalisedString : public CIwResource
{
	
public:
    IW_MANAGED_DECLARE(CLocalisedString);
    CLocalisedString()
    {
		ExampleInit();
	}

    ~CLocalisedString()
    {
    }
	
    // IwResource virtuals
    virtual bool ParseAttribute(CIwTextParserITX* pParser, const char* pAttrName)
    {
        for (int i=0; i<GetApp()->NUM_LANGUAGES; i++)
        {
            if (!stricmp(g_LanguageNames[i], pAttrName))
            {
                pParser->ReadString(m_Strings[i]);
                return true;
            }
        }

        return CIwResource::ParseAttribute(pParser, pAttrName);
    }

    virtual void ParseClose(CIwTextParserITX* pParser)
    {
        CIwManaged* pParent = pParser->GetObject(-1);
        if (CIwResGroup* pResGroup = dynamic_cast<CIwResGroup*>(pParent))
        {
            // Add ourselves to the current res group.
            pResGroup->AddRes(RESTYPE_LOCALISED_STRING, this);
        }
        else
        {
            CIwResource::ParseClose(pParser);
        }
    }

    virtual void Serialise()
    {
        CIwResource::Serialise();

        for (int i=0; i<GetApp()->NUM_LANGUAGES; i++)
        {
            m_Strings[i].Serialise();
        }
    }

    // Public interface
    const char* GetString(CIwUIDailyReport::LocaliseLanguages language)
    {
        if ((0 <= language) && (language <GetApp()-> NUM_LANGUAGES))
        {
            return m_Strings[language].c_str();
        }

        return NULL;
    }

private:
    // Member data
    CIwStringL m_Strings[2];
};
IW_MANAGED_IMPLEMENT_FACTORY(CLocalisedString);



//-----------------------------------------------------------------------------
const char* LocaliseCallback(const CIwPropertySet& propertySet)
{
    uint32 hash = 0;
    if (propertySet.GetProperty("localiseCaption", hash, true))
    {
        CLocalisedString* pLocString = (CLocalisedString*)
            IwGetResManager()->GetResHashed(hash,
                RESTYPE_LOCALISED_STRING, IW_RES_SEARCH_ALL_F);

        if (pLocString)
        {
            return pLocString->GetString(GetApp()->g_Localise);
        }
    }
    return NULL;
}




//-----------------------------------------------------------------------------
void ExampleInit()
{
    //Initialise the IwUI module
    IwUIInit();

    //IW_CLASS_REGISTER(CLocalisedString);

    //Set localilisation callback
	GetApp()->g_Localise = GetApp()->DEFAULT_LANGUAGE;
    IwUISetLocaliseCallback(LocaliseCallback);
	
	uint64 lastTime = s3eTimerGetMs();
    while (s3eDeviceCheckQuitRequest())
    {
        // Calculate an update time step.
        uint64 currentTime = s3eTimerGetMs();
        int32 updateTime = currentTime > lastTime ?
            MIN((int32)(currentTime - lastTime), 1000) : 0;
        lastTime = currentTime;

        // Update & render UI
       ExampleUpdate(updateTime);
        GetApp()->Render();

        // Standard device interaction
        s3eDeviceYield();
        s3eKeyboardUpdate();
        s3ePointerUpdate();
	}
   
}
//-----------------------------------------------------------------------------
void ExampleShutDown()
{
    delete IwGetUIController();
    delete IwGetUIView();

    IW_CLASS_REMOVE(CLocalisedString);

    //Terminate the IwUI module
    IwUITerminate();
}
//-----------------------------------------------------------------------------
bool ExampleUpdate(int32 deltaMS)
{
    static CIwUIDailyReport::LocaliseLanguages g_OldLocalise = GetApp()->DEFAULT_LANGUAGE;
	//GetApp()->g_Localise = GetApp()->LOCALISE_FRENCH;
    if (g_OldLocalise != GetApp()->g_Localise)
    {
        g_OldLocalise = GetApp()->g_Localise;
        IwGetUIView()->Reactivate();
    }

    //Update the controller (this will generate control events etc.)
    //IwGetUIController()->Update();

    //Update the view (this will do animations etc.) The SDK's example framework has a fixed
    //framerate of 20fps, so we pass that duration to the update function.
    //IwGetUIView()->Update(1000/20);
	GetApp()->Update(deltaMS);
    return true;
}
//-----------------------------------------------------------------------------
void ExampleRender()
{
	 IwGxClear(IW_GX_COLOUR_BUFFER_F | IW_GX_DEPTH_BUFFER_F);

    //Render the UI
    IwGetUIView()->Render();

    //Flush IwGx
    IwGxFlush();
    //Display the rendered frame
    IwGxSwapBuffers();
}
